package com.bahiaapk;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.ListActivity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.bahiaapk.customize.MatchListAdapter;
import com.bahiaapk.customize.PullToRefreshListView;
import com.bahiaapk.customize.PullToRefreshListView.OnRefreshListener;
import com.bahiaapk.dto.Championship;
import com.bahiaapk.dto.Match;
import com.bahiaapk.dto.Stadium;
import com.bahiaapk.dto.Team;

/**
 * @author Igor Faria
 *
 */

public class MatchActivity extends ListActivity {
	
	//private WebView webView;
	
	private Button allMatchesBTN;
	private Button homeMatchesBTN;
	private Button liveMatchBTN;
	private TextView allMatchesTV;
	private TextView homeMatchesTV;
	private TextView liveMatchTV;

	private List<Match> list;
	private ListAdapter adapterListMatch;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.match);


        
        allMatchesBTN = (Button)findViewById(R.id.allMatchesBTN);
        homeMatchesBTN = (Button)findViewById(R.id.homeMatchesBTN);
        liveMatchBTN = (Button)findViewById(R.id.liveMatchBTN);
        allMatchesTV = (TextView)findViewById(R.id.allMatchesTV);
        homeMatchesTV = (TextView)findViewById(R.id.homeMatchesTV);
        liveMatchTV = (TextView)findViewById(R.id.liveMatchTV);
        listAllVideo();
        // Set a listener to be invoked when the list should be refreshed.
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });
        
        
        scrollMyListViewToBottom();

    }
    
    
    private void scrollMyListViewToBottom() {
    	getListView().post(new Runnable() {
            @Override
            public void run() {
                // Select the last row so it will scroll into view...
            	getListView().setSelection(adapterListMatch.getCount() - 1);
            }
        });
    }
    
    private void listAllVideo() {
    	list = new ArrayList<Match>();
    	
    	Match match = null;
    	for (int i = 0; i < 12; i++) {
    		match = new Match();
    		match.setHomeTeam(new Team());
    		match.setAwayTeam(new Team());
    		match.setStadium(new Stadium());
    		match.setChampionship(new Championship());

    		match.setHomeGoals(2);
    		match.setAwayGoals(0);
    		match.getHomeTeam().setName("Bahia");
    		match.getAwayTeam().setName("Palmeiras");
    		match.getStadium().setName("Pituacu");
    		match.getChampionship().setName("Campeonato Brasileiro 2012");
    		match.setDate(new Date());

    		list.add(match);
		}
    	
    	adapterListMatch = (ListAdapter) new MatchListAdapter(this, list);        
        
	    setListAdapter(adapterListMatch);
	}
    

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                ;
            }
			return null;
           
        }

        @Override
        protected void onPostExecute(String[] result) {
        
            // Call onRefreshComplete when the list has been refreshed.
            ((PullToRefreshListView) getListView()).onRefreshComplete();

            super.onPostExecute(result);
        }
    }
    
    

    public void changeNews(View view){
		
    	allMatchesBTN.setBackgroundResource(Color.TRANSPARENT);
    	allMatchesTV.setTextColor(Color.BLACK);
		
		homeMatchesBTN.setBackgroundResource(Color.TRANSPARENT);
		homeMatchesTV.setTextColor(Color.BLACK);
		
		liveMatchBTN.setBackgroundResource(Color.TRANSPARENT);
		liveMatchTV.setTextColor(Color.BLACK);
		
		switch (view.getId()) {
            case R.id.allMatchesBTN: {
            	allMatchesBTN.setBackgroundResource(R.drawable.img_btn_news2);
        		allMatchesTV.setTextColor(Color.WHITE);
                break;
            }
                
            case R.id.homeMatchesBTN: {
            	homeMatchesBTN.setBackgroundResource(R.drawable.img_btn_news2);
            	homeMatchesTV.setTextColor(Color.WHITE);
            
                break;
            }
            
            case R.id.liveMatchBTN: {
            	liveMatchBTN.setBackgroundResource(R.drawable.img_btn_news2);
            	liveMatchTV.setTextColor(Color.WHITE);
            	
                break;
            }
            
           
		}
	}
}
